//
//  ZCTopicCell.m
//  ZCBaiSi
//
//  Created by zc on 16/8/22.
//  Copyright © 2016年 zc. All rights reserved.
//

#import "ZCTopicCell.h"
#import "ZCTopic.h"
#import <UIImageView+WebCache.h>
#import "ZCComment.h"
#import "ZCUser.h"
#import "ZCTopicPictureView.h"
#import "ZCTopicVideoView.h"
#import "ZCVoiceView.h"
@interface ZCTopicCell()
@property (weak, nonatomic) IBOutlet UIImageView *profileImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet UILabel *createdAtLabe;
@property (weak, nonatomic) IBOutlet UILabel *textLab;
@property (weak, nonatomic) IBOutlet UIButton *dingBtn;
@property (weak, nonatomic) IBOutlet UIButton *caiBtn;
@property (weak, nonatomic) IBOutlet UIButton *shareBtn;
@property (weak, nonatomic) IBOutlet UIButton *commentLab;
@property (weak, nonatomic) IBOutlet UIView *topCmtView;
@property (weak, nonatomic) IBOutlet UILabel *topCmtContentab;
@property (nonatomic, weak) ZCVoiceView *voiceView;
@property (nonatomic, weak) ZCTopicPictureView *pictureView;
@property (nonatomic, weak) ZCTopicVideoView *videoView;

@end

@implementation ZCTopicCell

- (ZCVoiceView *)voiceView{
    if (!_voiceView) {
        ZCVoiceView *voiceView = [ZCVoiceView viewFromXib];
        [self.contentView addSubview:voiceView];
        _voiceView = voiceView;
    }
    return _voiceView;
}

- (ZCTopicPictureView *)pictureView{
    if (!_pictureView) {
        ZCTopicPictureView *pictureView = [ZCTopicPictureView viewFromXib];
        [self.contentView addSubview:pictureView];
        _pictureView = pictureView;
    }
    return _pictureView;
}

- (ZCTopicVideoView *)videoView{
    if (!_videoView) {
        ZCTopicVideoView *videoView = [ZCTopicVideoView viewFromXib];
        [self.contentView addSubview:videoView];
        _videoView = videoView;
    }
    return _videoView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
}

- (IBAction)clickCollect:(id)sender {
    UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"标题" message:@"消息" preferredStyle:UIAlertControllerStyleActionSheet];
    [alertCon addAction:[UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        ZCLog(@"收藏");
    }]];
    [alertCon addAction:[UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        ZCLog(@"举报");
    }]];
    [alertCon addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }]];
    [self.window.rootViewController presentViewController:alertCon animated:NO completion:nil];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setTopic:(ZCTopic *)topic{
    _topic = topic;
    [self.profileImageView sd_setImageWithURL:[NSURL URLWithString:_topic.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    self.nameLab.text = topic.name;
    self.textLab.text = topic.text;
   // NSDate *date = [NSDate date];//手机当前时间
    
    self.createdAtLabe.text = topic.created_at;

    [self setUpButton:self.dingBtn number:topic.ding placeHolder:@"顶"];
    [self setUpButton:self.caiBtn number:topic.ding placeHolder:@"踩"];
    [self setUpButton:self.shareBtn number:topic.ding placeHolder:@"分享"];
    [self setUpButton:self.commentLab number:topic.ding placeHolder:@"评论"];
    //最热评论
    if(topic.top_cmt){//有最热评论
        self.topCmtView.hidden = NO;
        
        ZCComment *comment = topic.top_cmt;
        NSString *username = comment.user.username;//用户名
        NSString *content = comment.content;//评论内容
        self.topCmtContentab.text = [NSString stringWithFormat:@"%@ : %@",username,content];
    }else{
        self.topCmtView.hidden = YES;
    }
    //中间内容
#pragma mark --根据ZCTopic模型数据的情况来决定中间添加什么控件
    if (topic.type == ZCTopicTypeVideo) {//视频
        self.videoView.hidden = NO;
        self.voiceView.hidden = YES;
        self.pictureView.hidden = YES;
        self.videoView.frame = topic.contentFrame;
    }else if (topic.type == ZCTopicTypeWord){//段子
        self.videoView.hidden = YES ;
        self.voiceView.hidden = YES;
        self.pictureView.hidden = YES;
    }else if (topic.type == ZCTopicTypePicture){//图片
        self.videoView.hidden = YES;
        self.voiceView.hidden = YES;
        self.pictureView.hidden = NO;
        self.pictureView.frame = topic.contentFrame;
        self.pictureView.topic = topic;
    }else if (topic.type == ZCTopicTypeVoice){//音频
        self.videoView.hidden = YES;
        self.voiceView.hidden = NO;
        self.pictureView.hidden = YES;
        self.voiceView.frame = topic.contentFrame;
        self.voiceView.topic = topic;
    }
}

- (void)setUpButton:(UIButton *)button number:(NSInteger)number placeHolder:(NSString *)placeHoder{
    if (number > 10000) {
        [button setTitle:[NSString stringWithFormat:@"%.1f万",number/ 10000.0] forState:UIControlStateNormal];
    } else if (number > 0){
        [button setTitle:[NSString stringWithFormat:@"%zd",number] forState:UIControlStateNormal];
    }else{
        [button setTitle:placeHoder forState:UIControlStateNormal];
    }
}

//重写这个方法的目的，能够拦截所有设置cell frame的操作
- (void)setFrame:(CGRect)frame{
    frame.size.height -= ZCMargin;
//    frame.origin.x += ZCMargin;
//    frame.size.width -= 2 * ZCMargin;
    frame.origin.y += ZCMargin;
    [super setFrame:frame];
    
}

@end
